<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="房源ID" prop="id">
        <el-input v-model="dataForm.id" placeholder="房源ID"></el-input>
      </el-form-item>
      <el-form-item label="所属用户ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="所属用户ID"></el-input>
      </el-form-item>
      <el-form-item label="户号" prop="num">
        <el-input v-model="dataForm.num" placeholder="户号"></el-input>
      </el-form-item>
      <el-form-item label="房源标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="房源标题"></el-input>
      </el-form-item>
      <el-form-item label="租金" prop="rent">
        <el-input v-model="dataForm.rent" placeholder="租金"></el-input>
      </el-form-item>
      <el-form-item label="支付类型（1付一押一2付一押二3付一押三4付二押一5付三押一6付三押二7月付8半年付）" prop="payType">
        <el-input v-model="dataForm.payType" placeholder="支付类型（1付一押一2付一押二3付一押三4付二押一5付三押一6付三押二7月付8半年付）"></el-input>
      </el-form-item>
      <el-form-item label="户型" prop="type">
        <el-input v-model="dataForm.type" placeholder="户型"></el-input>
      </el-form-item>
      <el-form-item label="面积(平方米)" prop="area">
        <el-input v-model="dataForm.area" placeholder="面积(平方米)"></el-input>
      </el-form-item>
      <el-form-item label="装修类型(1:毛坯,2:简装,3:精装,4:豪装5：其他)" prop="decoration">
        <el-input v-model="dataForm.decoration" placeholder="装修类型(1:毛坯,2:简装,3:精装,4:豪装5：其他)"></el-input>
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-input v-model="dataForm.region" placeholder="区域"></el-input>
      </el-form-item>
      <el-form-item label="所在楼层(1:低,2:中，3:高)" prop="floor">
        <el-input v-model="dataForm.floor" placeholder="所在楼层(1:低,2:中，3:高)"></el-input>
      </el-form-item>
      <el-form-item label="房源用途(1:住宅,2:公寓,3:办公,4:商铺,5:写字楼)" prop="houseUse">
        <el-input v-model="dataForm.houseUse" placeholder="房源用途(1:住宅,2:公寓,3:办公,4:商铺,5:写字楼)"></el-input>
      </el-form-item>
      <el-form-item label="朝向(1东2南3西4北)" prop="orientation">
        <el-input v-model="dataForm.orientation" placeholder="朝向(1东2南3西4北)"></el-input>
      </el-form-item>
      <el-form-item label="挂牌时间" prop="listTime">
        <el-input v-model="dataForm.listTime" placeholder="挂牌时间"></el-input>
      </el-form-item>
      <el-form-item label="房源核验码" prop="houseCode">
        <el-input v-model="dataForm.houseCode" placeholder="房源核验码"></el-input>
      </el-form-item>
      <el-form-item label="配套设施" prop="facilities">
        <el-input v-model="dataForm.facilities" placeholder="配套设施"></el-input>
      </el-form-item>
      <el-form-item label="房东要求" prop="need">
        <el-input v-model="dataForm.need" placeholder="房东要求"></el-input>
      </el-form-item>
      <el-form-item label="房源描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="房源描述"></el-input>
      </el-form-item>
      <el-form-item label="房源图片URL地址（最多10张，用逗号分隔）" prop="imageUrls">
        <el-input v-model="dataForm.imageUrls" placeholder="房源图片URL地址（最多10张，用逗号分隔）"></el-input>
      </el-form-item>
      <el-form-item label="房屋来源（官方平台，优居等）" prop="sourceId">
        <el-input v-model="dataForm.sourceId" placeholder="房屋来源（官方平台，优居等）"></el-input>
      </el-form-item>
      <el-form-item label="状态(0:未审核,1:审核中,2:二级:审核中,3:三级审核中,4:已上架,5:已出租,6:已下架,7:审核失败)" prop="state">
        <el-input v-model="dataForm.state" placeholder="状态(0:未审核,1:审核中,2:二级:审核中,3:三级审核中,4:已上架,5:已出租,6:已下架,7:审核失败)"></el-input>
      </el-form-item>
      <el-form-item label="出租类型（1整租，2分租）" prop="rentalType">
        <el-input v-model="dataForm.rentalType" placeholder="出租类型（1整租，2分租）"></el-input>
      </el-form-item>
      <el-form-item label="房屋详情来源" prop="source">
        <el-input v-model="dataForm.source" placeholder="房屋详情来源"></el-input>
      </el-form-item>
      <el-form-item label="房源权属（0：企业,1:个人，2：居间）" prop="ownershipType">
        <el-input v-model="dataForm.ownershipType" placeholder="房源权属（0：企业,1:个人，2：居间）"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="几居室" prop="typeNum">
        <el-input v-model="dataForm.typeNum" placeholder="几居室"></el-input>
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改者" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="修改者"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  userId: "",
  num: "",
  title: "",
  rent: "",
  payType: "",
  type: "",
  area: "",
  decoration: "",
  region: "",
  floor: "",
  houseUse: "",
  orientation: "",
  listTime: "",
  houseCode: "",
  facilities: "",
  need: "",
  description: "",
  imageUrls: "",
  sourceId: "",
  state: "",
  rentalType: "",
  source: "",
  ownershipType: "",
  address: "",
  typeNum: "",
  creator: "",
  createTime: "",
  updater: "",
  updateTime: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/rental/areaplothouse/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/rental/areaplothouse", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>